package com.leyou.item.server;


import com.leyou.ExceptionEnum.ExceptionEnum;
import com.leyou.exception.LyException;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.pojo.CategoryDTO;
import com.leyou.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;


@Service
public class CategoryService {
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    public List<CategoryDTO> queryByParentId(Long pid) {
        Category category = new Category();
        category.setParentId(pid);
        List<Category> select = categoryMapper.select(category);
        if (CollectionUtils.isEmpty(select)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(select,CategoryDTO.class);
    }

    public List<CategoryDTO> queryByBrandId(Long id) {
        List<Category> list = categoryMapper.queryByBrandId(id);
        if(CollectionUtils.isEmpty(list)){
            throw  new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,CategoryDTO.class);
    }


    public List<CategoryDTO> queryCategoryByIds(List<Long> categoryIds) {
        List<Category> list = categoryMapper.selectByListIds(categoryIds);


        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,CategoryDTO.class);
    }
}
